import CryptoJS from 'crypto-js';

const SECRET_KEY = '514ec1df39a04ef7';

export function encrypt(data) {
    const jsonString = typeof data === 'string' ? data : JSON.stringify(data);
    const ciphertext = CryptoJS.AES.encrypt(jsonString, SECRET_KEY).toString();
    return encodeURIComponent(ciphertext);
}

export function decrypt(encryptedData) {
    try {
        const decoded = decodeURIComponent(encryptedData);
        const bytes = CryptoJS.AES.decrypt(decoded, SECRET_KEY);
        const plaintext = bytes.toString(CryptoJS.enc.Utf8);
        if (!plaintext) return null;
        return JSON.parse(plaintext);
    } catch (e) {
        console.error('解密失败:', e);
        return null;
    }
}